/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.app;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import chunmap.app.bench.Command;
import chunmap.app.bench.OperateManager;
import chunmap.app.bench.Tool;
import chunmap.util.CMEventListenerList;
import chunmap.util.Function;
import chunmap.view.Map;
import chunmap.view.View;
import chunmap.view.layer.LayerCollection;
/**
 * 组合所有地图功能
 * @author yangjiandong
 *
 */
public class MapCtrlImpl implements MapCtrl{
	
	private Map map;
	private Image bufferImage;
	//private RenderThread thread;
	private OperateManager operateManager;
	private Component component;
	
	public CMEventListenerList listeners=new CMEventListenerList();

	public MapCtrlImpl(Component component) {
		super();
		this.component=component;
		init(component.getWidth(), component.getHeight());
		operateManager=new OperateManager(this,component);
	}
	
	private void init(int width, int height) {
		
		map = new Map(width, height);
		bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
//		thread = new RenderThread();
//		thread.addListener(new RenderListener() {
//			@Override
//			public void renderFinished() {
//				makeBuffer();
//			}
//		});
	}

	private void makeBuffer() {
		Image nimage = (Image)map.getPainter().getData();
		bufferImage.getGraphics().drawImage(nimage, 0, 0, null);
		bufferImage.flush();
		component.repaint();
	}

	

	@Override
	public void refreshMap() {
		//thread.startRender(map);
		map.render();
		makeBuffer();
	}

	@Override
	public Image getBufferImage() {
		return bufferImage;
	}
	
	//---------------------------------------------------------代理

	@Override
	public LayerCollection getLayerCollection() {
		return map.getLayerCollection();
	}

	@Override
	public View getView() {
		return map.getView();
	}

	@Override
	public void fullView() {
		map.fullView();
	}

	@Override
	public void executeCommand(Command cmd) {
		operateManager.executeCommand(cmd);		
	}

	@Override
	public void addPlug(Tool plug) {
		operateManager.addPlug(plug);	
	}

	@Override
	public Tool getPlug(String id) {
		return operateManager.getPlug(id);
	}

	@Override
	public void redo() {
		operateManager.redo();		
	}

	@Override
	public void undo() {
		operateManager.undo();		
	}

	@Override
	public boolean imageUpdate(Image img, int infoflags, int x, int y,
			int w, int h) {
		return component.imageUpdate(img, infoflags, x, y, w, h);
	}

	@Override
	public Graphics getGraphics(){
		return component.getGraphics();
	}

	@Override
	public void setCurrentPlug(Tool plug) {
		operateManager.setCurrentPlug(plug);
	}

	@Override
	public Image getOriginImage() {
		return (Image)map.getPainter().getData();
	}

	@Override
	public Graphics getBufferGraphics() {
		return bufferImage.getGraphics();
	}

	@Override
	public void resetSize(int width, int height) {
		map.resetSize(width, height);
        init(width, height);
	}

	@Override
	public void addListener(Function func) {
		listeners.add(func);
	}

}
